package cn.tqfeiyang.springplus.warehouse.service.impl;

import cn.tqfeiyang.springplus.warehouse.entity.SupplierEntity;
import cn.tqfeiyang.springplus.warehouse.mapper.SupplierMapper;
import cn.tqfeiyang.springplus.warehouse.service.SupplierService;
import cn.tqfeiyang.springplus.warehouse.request.SupplierDTO;
import cn.tqfeiyang.springplus.warehouse.response.SupplierVO;
import cn.tqfeiyang.springplus.warehouse.request.SupplierSTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.tqfeiyang.springplus.framework.conversion.ConversionService;
import cn.tqfeiyang.springplus.framework.exception.BusinessException;
import cn.tqfeiyang.springplus.framework.response.ResultCode;
import cn.tqfeiyang.springplus.framework.pagination.PageHelper;
import cn.tqfeiyang.springplus.framework.pagination.PageData;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;

/**
 * 供应商服务实现类
 *
 * @author tqfeiyang
 * @since 2024-08-01
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, SupplierEntity> implements SupplierService {

    /**
     * 对象转换服务
     */
    @Resource
    private ConversionService conversionService;

    /**
     * 供应商映射接口
     */
    @Resource
    private SupplierMapper supplierMapper;

    /**
     * 分页查询供应商
     *
     * @param supplierSTO 查询数据
     * @return 供应商列表
     */
    @Override
    public PageData<SupplierVO> selectSupplierPage(SupplierSTO supplierSTO) {
        Assert.notNull(supplierSTO, "supplierSTO不能为空");
        PageHelper<SupplierEntity> pageHelper = new PageHelper<>(supplierSTO);
        return pageHelper.selectPage(supplierMapper, SupplierVO.class);
    }

    /**
     * 查询供应商
     *
     * @param id 供应商id
     * @return 供应商信息
     */
    @Override
    public SupplierVO selectSupplierInfo(Long id) {
        Assert.notNull(id, "id不能为空");
        SupplierEntity supplierEntity = checkSupplierExists(id);
        return conversionService.map(supplierEntity, SupplierVO.class);
    }

    /**
     * 新建供应商
     *
     * @param supplierDTO 请求数据
     * @return 供应商id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createSupplier(SupplierDTO supplierDTO) {
        Assert.notNull(supplierDTO, "supplierDTO不能为空");
        SupplierEntity supplierEntity = conversionService.map(supplierDTO, SupplierEntity.class);
        if (supplierMapper.insert(supplierEntity) == 0) {
            throw new BusinessException(ResultCode.ENTITY_INSERT_ERROR);
        }
        return supplierEntity.getId();
    }

    /**
     * 修改供应商
     *
     * @param id 供应商id
     * @param supplierDTO 请求数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSupplier(Long id, SupplierDTO supplierDTO) {
        Assert.notNull(id, "id不能为空");
        Assert.notNull(supplierDTO, "supplierDTO不能为空");
        SupplierEntity originalEntity = checkSupplierExists(id);
        SupplierEntity supplierEntity = conversionService.map(supplierDTO, SupplierEntity.class);
        supplierEntity.setId(id);
        supplierEntity.setVersion(originalEntity.getVersion());
        if (supplierMapper.updateById(supplierEntity) == 0) {
            throw new BusinessException(ResultCode.ENTITY_UPDATE_ERROR);
        }
    }

    /**
     * 删除供应商
     *
     * @param id 供应商id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSupplier(Long id) {
        Assert.notNull(id, "id不能为空");
        checkSupplierExists(id);
        if (supplierMapper.deleteById(id) == 0) {
            throw new BusinessException(ResultCode.ENTITY_DELETE_ERROR);
        }
    }

    /**
     * 检查供应商实体是否存在
     *
     * @param id 供应商id
     * @return 供应商实体
     */
    protected SupplierEntity checkSupplierExists(Long id) {
        SupplierEntity supplierEntity = supplierMapper.selectById(id);
        if (supplierEntity == null) {
            throw new BusinessException(ResultCode.ENTITY_NOT_EXISTS);
        }
        return supplierEntity;
    }

}
